import * as example from './action-type'
import initState from './state'

//封装后的写法
var reducers = {
    [example.COUNTADD]:(state , action={}) => {
        state.count++;
        return Object.assign({}, state)
    },
    [example.COUNTDEL]:(state , action={}) => {
        state.count--;
        return Object.assign({}, state)
    },
    [example.COUNTSET]:(state , action={}) => {
        state.count = action.value;
        return Object.assign({}, state)
    },
    default:(state , action={})=>state
};

//不管 怎么着, 当前的方法返回的必须是一个
export const exampleRepertory = (state = initState, action = {}) => {
    if(reducers[action.type]){
        return reducers[action.type](state,action)
    }
    return reducers['default'](state,action);
};


// 常用的写法
// export const exampleRepertory = (state = initState, action = {}) => {
//     switch (action.type) {
//         case example.COUNTADD:
//             return ((state, action = {}) => {
//                 state.count++;
//                 return Object.assign({}, state)
//             })(state,action);
//         case example.COUNTDEL:
//             return ((state, action = {}) => {
//                 state.count--;
//                 return Object.assign({}, state)
//             })(state,action);
//         case example.COUNTSET:
//             return ((state, action = {}) => {
//                 state.count = action.value;
//                 return Object.assign({}, state)
//             })(state,action);
//         default:
//             return state;
//     }
// };

